<template>
    <el-dialog
    title="消息"
    :visible="show"
    @close="$emit('close')"
    width="550px"
    >
        <div class="msg-box">
            <el-card 
            v-for="(item, index) in msgs"
            :key="item.createTime"
            >
                <div class="msg">
                    <img :src="`${$API+item.friend_info.avatar}`" alt="">
                    <div class="user-search-info">
                        <div class="user-search-name">{{item.friend_info.username}}</div>
                        <div class="user-search-userid">用户ID：{{item.friend_info.userid}}</div>
                        <div class="user-search-notify">{{item.msg}}</div>
                    </div>
                    <el-tooltip class="item" effect="dark" content="添加好友" placement="bottom-end">
                        <i 
                        class="el-icon-plus adduser"
                        @click="addFriend(item, index)"
                        ></i>
                    </el-tooltip>
                </div>
            </el-card>
            <el-alert
            v-if="msgs.length === 0"
            title="暂无新消息"
            type="error"
            effect="dark" />
        </div>
    </el-dialog>
</template>

<script>
import axios from "axios";

export default {
  name: "msg-modal",
  props: {
    show: {
      default: true,
      type: Boolean,
    },
  },
  computed: {
      msgs() {
          return this.$_.msgs
      }
  },
  methods: {
    addFriend(item, index) {
        axios.post(`${this.$API}/passFriend`, {
            userid: item.userid,
            friend: item.friend
        }).then(res => {
            if (res.data.code === 200) {
                this.$_.msgs.splice(index, 1)
                this.$message.success(res.data.msg);
                const newfriend = res.data.data.find(s => s.userid != item.userid)
                this.$_.users.push({
                    ...newfriend,
                    recent: '',
                    msg: []
                })
            }
        })
    }
  }
}
</script>

<style scoped>
.msg-box {
    height: 400px;
    overflow: auto;
}
.msg {
    display: flex;
    padding: 10px 0 ;
    align-items: center;
}
.msg img {
  width: 80px;
  height: 80px;
}
.user-search-info {
  flex: 1;
  padding: 0 10px;
}
.user-search-name {
  font-size: 24px;
  font-weight: bold;
}
.user-search-userid {
    font-size: 12px;
    color: #949494;
}
.user-search-notify {
    font-size: 14px;
    color: #000;
}
.adduser {
  font-size: 24px;
  cursor: pointer;
}
</style>